package com.ruoyi.coal.modules.coalpile.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.coal.common.utils.PageUtils;
import com.ruoyi.coal.common.utils.Query;
import com.ruoyi.coal.modules.coalpile.domain.CoalPileInfoEntity;
import com.ruoyi.coal.modules.coalpile.domain.CoalYardHistoryEntity;
import com.ruoyi.coal.modules.coalpile.domain.PointEntity;
import com.ruoyi.coal.modules.coalpile.domain.vo.CoalPileSiloVo;
import com.ruoyi.coal.modules.coalpile.domain.vo.CoalYardHistoryVo;
import com.ruoyi.coal.modules.coalpile.domain.vo.ComparePilesVo;
import com.ruoyi.coal.modules.coalpile.mapper.CoalPileInfoDao;
import com.ruoyi.coal.modules.coalpile.mapper.CoalYardHistoryDao;
import com.ruoyi.coal.modules.coalpile.mapper.CoalYardInfoDao;
import com.ruoyi.coal.modules.coalpile.service.CoalPileInfoService;
import com.ruoyi.coal.modules.coalpile.service.CoalYardHistoryService;
import com.ruoyi.coal.modules.coalpile.service.PointService;
import com.ruoyi.coal.modules.minio.MinioConfig;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("coalYardHistoryService")
public class CoalYardHistoryServiceImpl extends ServiceImpl<CoalYardHistoryDao, CoalYardHistoryEntity> implements CoalYardHistoryService {
    @Autowired
    private CoalYardHistoryDao coalYardHistoryDao;

    @Autowired
    CoalYardHistoryService coalYardHistoryService;

    @Autowired
    PointService pointService;

    @Autowired
    private MinioConfig minioConfig;

    @Autowired
    private CoalYardInfoDao coalYardInfoDao;

    @Autowired
    private CoalPileInfoDao coalPileInfoDao;

    @Autowired
    private CoalPileInfoService coalPileInfoService;

    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<CoalYardHistoryEntity> wrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty((String)params.get("key")))
            wrapper.eq("coal_yard_id", params.get("key"));
        IPage<CoalYardHistoryEntity> page = page((new Query())
                .getPage(params), (Wrapper)wrapper
                .eq("measure_type", params.get("measure_type")));
        return new PageUtils(page);
    }

    public CoalYardHistoryVo getAllNew(Integer coalYardId) {
        QueryWrapper<CoalYardHistoryEntity> CoalYardWrapper = new QueryWrapper();
        ((QueryWrapper)((QueryWrapper)CoalYardWrapper.eq("coal_yard_id", coalYardId)).orderBy(true, false, (Object[])new String[] { "time" })).eq("measure_type", Integer.valueOf(1));
        List<CoalYardHistoryEntity> coalYardHistoryEntities = this.coalYardHistoryService.list((Wrapper)CoalYardWrapper);
        if (coalYardHistoryEntities.size() != 0) {
            CoalYardHistoryEntity coalYardHistoryEntity = coalYardHistoryEntities.get(0);
            CoalYardHistoryVo coalYardHistoryVo = new CoalYardHistoryVo();
            BeanUtils.copyProperties(coalYardHistoryEntity, coalYardHistoryVo);
            coalYardHistoryVo.setMaxPoint((PointEntity)this.pointService.getById(coalYardHistoryEntity.getMaxPoint()));
            coalYardHistoryVo.setMinPoint((PointEntity)this.pointService.getById(coalYardHistoryEntity.getMinPoint()));
            return coalYardHistoryVo;
        }
        return null;
    }

    public List<ComparePilesVo> comparePiles(Integer[] ids) {
        ArrayList<ComparePilesVo> vos = new ArrayList<>();
        for (Integer historyId : ids) {
            ComparePilesVo vo = new ComparePilesVo();
            CoalYardHistoryEntity historyEntity = (CoalYardHistoryEntity)this.coalYardHistoryDao.selectById(historyId);
            PointEntity maxPoint = (PointEntity)this.pointService.getById(historyEntity.getMaxPoint());
            PointEntity minPoint = (PointEntity)this.pointService.getById(historyEntity.getMinPoint());
            vo.setId(historyId);
            vo.setPointEntitymax(maxPoint);
            vo.setPointEntitymin(minPoint);
            vo.setUrlPcd(historyEntity.getUrl());
            vos.add(vo);
        }
        return vos;
    }

    public List<CoalPileSiloVo> silodisplay() {
        ArrayList<CoalPileSiloVo> vos = new ArrayList<>();
        for (String coalYardName : this.coalYardInfoDao.getCoalYardName()) {
            QueryWrapper<CoalPileInfoEntity> wrapper = new QueryWrapper();
            ((QueryWrapper)wrapper.eq("name", coalYardName)).orderByDesc("create_time");
            if (this.coalPileInfoDao.selectList((Wrapper)wrapper).size() != 0) {
                CoalPileInfoEntity coalPileInfoEntity = (CoalPileInfoEntity) this.coalPileInfoDao.selectList((Wrapper)wrapper).get(0);
                CoalPileSiloVo vo = new CoalPileSiloVo();
                BeanUtils.copyProperties(coalPileInfoEntity, vo);
                vo.setPointEntitymax((PointEntity)this.pointService.getById(coalPileInfoEntity.getMaxPoint()));
                vo.setPointEntitymin((PointEntity)this.pointService.getById(coalPileInfoEntity.getMinPoint()));
                vos.add(vo);
            }
        }
        return vos;
    }

    public String deletePreviousUrl(Long day) {
        LocalDate currentDate = LocalDate.now();
        LocalDate nDaysAgo = currentDate.minusDays(day.longValue());
        QueryWrapper<CoalYardHistoryEntity> wrapper = new QueryWrapper();
        ((QueryWrapper)wrapper.eq("del", Integer.valueOf(1))).lt("time", nDaysAgo);
        List<CoalYardHistoryEntity> coalYardHistoryEntities = this.coalYardHistoryDao.selectList((Wrapper)wrapper);
        if (coalYardHistoryEntities.isEmpty())
            return "文件已为空删除失败";
        Integer[] coalYardHistoryIdArray = (Integer[])coalYardHistoryEntities.stream().map(x -> x.getId()).toArray(x$0 -> new Integer[x$0]);
        String msg = deleteHistoryData(coalYardHistoryIdArray);
        return msg;
    }

    public String deleteHistoryData(Integer[] ids) {
        List<String> urlList = new ArrayList<>();
        List<Integer> coalPileIds = new ArrayList<>();
        List<Integer> pointIds = new ArrayList<>();
        QueryWrapper<CoalYardHistoryEntity> wrapper = new QueryWrapper();
        wrapper.in("id", Arrays.asList(ids));
        List<CoalYardHistoryEntity> coalYardHistoryEntities = this.coalYardHistoryDao.selectList((Wrapper)wrapper);
        for (CoalYardHistoryEntity entity : coalYardHistoryEntities) {
            urlList.add(entity.getUrl());
            urlList.add(entity.getUrl().replace(".pcd", "_all.pcd"));
            pointIds.add(entity.getMinPoint());
            pointIds.add(entity.getMinPoint());
        }
        List<Integer> idList = (List<Integer>)coalYardHistoryEntities.stream().map(CoalYardHistoryEntity::getId).collect(Collectors.toList());
        for (Integer id : idList) {
            for (CoalPileInfoEntity coalPileInfoEntity : this.coalYardHistoryDao.selectHistoryByYardId(id)) {
                coalPileIds.add(coalPileInfoEntity.getId());
                pointIds.add(coalPileInfoEntity.getMinPoint());
                pointIds.add(coalPileInfoEntity.getMinPoint());
                if (!StringUtils.isEmpty(coalPileInfoEntity.getUrlGltf()))
                    urlList.add(coalPileInfoEntity.getUrlGltf());
            }
        }
        for (String url : urlList) {
            String[] parts = url.split("/");
            String result = String.join("/", Arrays.<CharSequence>copyOfRange((CharSequence[])parts, 4, parts.length));
            System.out.println("----------");
            System.out.println("删除的文件为"+ result);
            this.minioConfig.removeObject(result);
        }
        this.coalYardHistoryService.removeByIds(Arrays.asList(ids));
        this.coalPileInfoService.removeByIds(coalPileIds);
        this.pointService.removeByIds(pointIds);
        return "删除文件";
    }
}

